package com.cenwangkeji.resident.ui.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.GxyBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.SpecialDiseaseDetailAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;

import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//高血压详情
public class GXYDetailActivity extends BaseActivity {


    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_xer_detail;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    private void initRecycelrView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<GxyBean>() {
            @Override
            public void subscribe(ObservableEmitter<GxyBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post(jsonObject.toString(),Constant.gxy_detail);
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, GxyBean.class));


            }
        }).compose(RxUtils.<GxyBean>io_main()).subscribe(new ObserverOnNext<GxyBean>() {
            @Override
            public void onNext(GxyBean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("症状：\n" + zz(bean.getData().getGxyZzList(), bean.getData().getGxyTzQt()));
                        list.add("血压(mmHg):收缩率: " + bean.getData().getGxyTzXyss() + "      舒张率: " + bean.getData().getGxyTzXysz());
                        list.add("体重：" + bean.getData().getGxyTzTz());
                        list.add("体质指数（BMI）kg/m²: " + bean.getData().getGxyTzTzzs());
                        list.add("心率(次/分钟)：" + bean.getData().getGxyTzXl());
                        list.add("其他：" + bean.getData().getGxyTzQt());
                        list.add("日吸烟量(支)：" + bean.getData().getGxyShfszdRxyl());
                        list.add("日饮酒量(两)：" + bean.getData().getGxyShfszdRyjl());
                        list.add("运动：" + bean.getData().getGxyShfszdZyd() + " 次/周    " + bean.getData().getGxyShfszdCyd() + " 分钟/次");
                        list.add("摄盐情况：" + syqk(bean.getData().getGxyShfszdSyqk()));
                        list.add("心理调整：" + xltz(bean.getData().getGxyShfszdXltz()));
                        list.add("遵医行为：" + xltz(bean.getData().getGxyShfszdZyxw()));
                        list.add("辅助检查：" + bean.getData().getFzjc());
                        list.add("服药依从性：" + fyycx(bean.getData().getGxyFyycx()));
                        list.add("不良反应：" + (bean.getData().getGxyYwblfy() == 1 ? "" : "有 " + bean.getData().getGxyYwblfyName()));
                        list.add("此次随访分类：" + sffl(bean.getData().getGxyCcsffl()));
                        if (!TextUtils.isEmpty(bean.getData().getGxyYyqkYwmc1())) {
                            list.add("药物名称：" + bean.getData().getGxyYyqkYwmc1() + "\n"
                                    + "     每日次数：" + bean.getData().getGxyYyqkMejc1()
                                    + "     每次几颗：" + bean.getData().getGxyYyqkMcjk1());
                        }
                        if (!TextUtils.isEmpty(bean.getData().getGxyYyqkYwmc2())) {
                            list.add("药物名称：" + bean.getData().getGxyYyqkYwmc2() + "\n"
                                    + "     每日次数：" + bean.getData().getGxyYyqkMejc2()
                                    + "     每次几颗：" + bean.getData().getGxyYyqkMcjk2());
                        }
                        if (!TextUtils.isEmpty(bean.getData().getGxyYyqkYwmc3())) {
                            list.add("药物名称：" + bean.getData().getGxyYyqkYwmc3() + "\n"
                                    + "     每日次数：" + bean.getData().getGxyYyqkMejc3()
                                    + "     每次几颗：" + bean.getData().getGxyYyqkMcjk3());
                        }
                        if (bean.getData().getZzjy() == 1) {
                            list.add("转诊原因：" + bean.getData().getZzyy());
                            list.add("转诊机构：" + bean.getData().getZzjg());
                            list.add("转诊科室：" + bean.getData().getZzks());
                        }
                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //此次随访分类
    private String sffl(int index){
        String xl =" ";
        switch (index){
            case 1:
                xl = " 控制满意 ";
                break;
            case 2:
                xl = " 控制不满意 ";
                break;
            case 3:
                xl = " 不良反应 ";
                break;
            case 4:
                xl = " 并发症 ";
                break;
        }
        return xl;
    }

    //服药依从性
    private String fyycx(int index){
        String xl =" ";
        switch (index){
            case 1:
                xl = " 规律 ";
                break;
            case 2:
                xl = " 间断 ";
                break;
            case 3:
                xl = " 不服药 ";
                break;
        }
        return xl;
    }




    //心理调整
    private String xltz(int index){
        String xl =" ";
        switch (index){
            case 1:
                xl = " 良好 ";
                break;
            case 2:
                xl = " 一般 ";
                break;
            case 3:
                xl = " 差 ";
                break;
        }
        return xl;
    }

    //摄盐情况
    private String syqk(int index){
        String sy =" ";
        switch (index){
            case 1:
                sy = " 轻 ";
                break;
            case 2:
                sy = " 中 ";
                break;
            case 3:
                sy = " 重 ";
                break;
        }
        return sy;
    }


    //症状
    private String zz(List<String> list, String qt){
        String zz ="";
        for (int i = 0; i<list.size(); i++){
            switch (list.get(i)){
                case "1":
                    zz += "无症状 ";
                    break;
                case "2":
                    zz += "头晕恶心 ";
                    break;
                case "3":
                    zz+= "恶心呕吐 ";
                    break;
                case "4":
                   zz += "眼花耳鸣 ";
                    break;
                case "5":
                    zz += "呼吸困难 ";
                    break;
                case "6":
                    zz += "心悸胸闷 ";
                    break;
                case "7":
                    zz += "鼻衄出血不止 ";
                    break;
                case "8":
                    zz += "四肢发麻 ";
                    break;
                case "9":
                    zz += "下肢水肿 ";
                    break;
                case "10":
                    zz += qt;
                    break;
            }
        }
        return zz;

    }
}
